package compiler;

import java.util.*;
import java.io.*;

/**
 *Verifies that there is exaclty one argument as the infut file and that 
 *it has the .lol extension. If the file is in correct format it writes 
 *the entire file into one string. Otherwise it exits the program.
 *Once it has string containing the entire files content it runs through
 *and makes sure there are no lexical, syntax, or semantic errors. If it 
 *comes across an error, the program is exited and no output file is created.
 *If it makes it through without any errors it is then written to a file that is
 *created in the same directory with the same name but with .html extension.
 * @author Zach Sierakowski
 *
 */
public class MyCompiler {

	public static String entireInput;
	public static String token;
	public static String html;
	static MyLexicalAnalyzer Lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer generator;
	static ArrayList<String> parseTree;
	static ArrayList<DictionaryTokenTuple<String,String>> possibleTokens;
		
	public static void main(String[] args) {
		
		//Check that there is 1 argument
		if(args.length != 1){
			System.err.println("ERROR! There is not a single argument");
			System.exit(0);
		}	
		
		Scanner inputFile= null;
		File file = null;
		try {
			file = new File(args[0]);
			inputFile = new Scanner(file);
		} catch (FileNotFoundException e) {
			System.err.print("File not found");
			e.printStackTrace();
		}

		
		//check extension
		String extension = "";
		
		String fileName = args[0];

		int i = fileName.lastIndexOf('.');
		if (i > 0) {
		    extension = fileName.substring(i+1);
		}
		
		if(!extension.equals("lol")){
			System.err.print("ERROR! Incorrect file extension");
			System.exit(0);
		}
		
		entireInput = "";
		while(inputFile.hasNext()){
			entireInput += inputFile.nextLine();
		}				
		inputFile.close();
				
		
		//initialize parse tree array
		parseTree = new ArrayList<String>();
		//initialize and create possible tokens
		possibleTokens = new ArrayList<DictionaryTokenTuple<String,String>>();
		
		possibleTokens.add(TokenDictionary.DOC_BEGIN);
		possibleTokens.add(TokenDictionary.DOC_END);
		possibleTokens.add(TokenDictionary.HEAD_BEGIN);
		possibleTokens.add(TokenDictionary.HEAD_END);
		possibleTokens.add(TokenDictionary.COMMENT_BEGIN);
		possibleTokens.add(TokenDictionary.COMMENT_END);
		possibleTokens.add(TokenDictionary.TITLE_BEGIN);
		possibleTokens.add(TokenDictionary.TITLE_END);
		possibleTokens.add(TokenDictionary.BOLD_BEGIN);
		possibleTokens.add(TokenDictionary.BOLD_END);
		possibleTokens.add(TokenDictionary.ITALICS_BEGIN);
		possibleTokens.add(TokenDictionary.ITALICS_END);
		possibleTokens.add(TokenDictionary.LIST_BEGIN);
		possibleTokens.add(TokenDictionary.LIST_END);
		possibleTokens.add(TokenDictionary.ITEM_BEGIN);
		possibleTokens.add(TokenDictionary.ITEM_END);
		possibleTokens.add(TokenDictionary.DEFINE_BEGIN);
		possibleTokens.add(TokenDictionary.DEFINE_END);
		possibleTokens.add(TokenDictionary.USE_BEGIN);
		possibleTokens.add(TokenDictionary.USE_END);
		possibleTokens.add(TokenDictionary.ITZ);
		possibleTokens.add(TokenDictionary.VIDEO_BEGIN);
		possibleTokens.add(TokenDictionary.VIDEO_END);
		possibleTokens.add(TokenDictionary.AUDIO_BEGIN);
		possibleTokens.add(TokenDictionary.AUDIO_END);
		possibleTokens.add(TokenDictionary.PARAGRAPH_BEGIN);
		possibleTokens.add(TokenDictionary.PARAGRAPH_END);
		possibleTokens.add(TokenDictionary.NEWLINE);
		
		token = "";
		
		Lexer = new MyLexicalAnalyzer();
		Lexer.getNextToken();
		
		parser = new MySyntaxAnalyzer();
		parser.lolcode();
		
		//html variable for the output file
		html = "";
		
		generator = new MySemanticAnalyzer();
		generator.convert();
		

		//get filename and change the extension to .html
		String filename = args[0];
		filename = filename.substring(0, filename.lastIndexOf("."));
		filename += ".html";
		
		
		//write the output file
		BufferedWriter writer = null;
        try {
            String nameOfFile = filename;
            File logFile = new File(nameOfFile);

            writer = new BufferedWriter(new FileWriter(logFile));
            writer.write(html);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
            }
        }
        
        System.out.println("Successfully Compiled... " + filename + " is now in the same directory as the " + args[0] + " file!");
	}

}
